package com.yang.controller;

import com.yang.entity.UserThings;
import com.yang.service.NoteThingsService;
import com.yang.service.UserThingsService;
import com.yang.utils.PictureFactory;
import com.yang.entity.EmailSmsCountInfo;
import com.yang.utils.info.PictureDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author yangdacheng
 * @title: PictureController 用来获取图表
 * @projectName note_api
 * @description: TODO
 * @date 2022/3/25 11:07 上午
 */
@RestController
@RequestMapping("pictureTable")
@Slf4j
public class PictureController {

    @Resource
    private PictureFactory pictureFactory;
    @Resource
    private NoteThingsService noteThingsService;
    @Resource
    private UserThingsService userThingsService;

    /**
     * 获取图1的数据/图2
     * @param userID
     * @return
     */
    @GetMapping("/getPictureOne/{uid}")
    public List<PictureDataInfo> queryOnePicture(@PathVariable("uid") String userID){
        List<PictureDataInfo> pictureDataList = new ArrayList<>(); //用来存放放回的集合
        PictureDataInfo pictureDataEmail = new PictureDataInfo(); //用来存放邮箱发送数据
        PictureDataInfo pictureDataSms = new PictureDataInfo(); //用来存放短信发送数据
        PictureDataInfo pictureDataPunch = new PictureDataInfo(); //用来存放打卡率
        PictureDataInfo pictureDate = new PictureDataInfo(); //用来存放日期
        List<Object> emailDataList = new ArrayList<>(); //用来存放邮箱对应的数据集合
        List<Object> smsDataList = new ArrayList<>(); //用来存放短信对应的集合
        List<Object> punchDataList = new ArrayList<>(); //用来存放打卡率数据集合
        List<Object> dateList = new ArrayList<>(); //用来存放日期数据集合
        List<EmailSmsCountInfo> emailSmsCountInfos = pictureFactory.queryOneData(userID); //获取数据
        emailSmsCountInfos.forEach((value)->{
           //设置邮箱对应信息
            emailDataList.add(value.getEmailCount()); //设置对应数量
            dateList.add(value.getDataLabel()); //获取日期
        });
        pictureDataEmail.setValues(emailDataList); //设置集合
        pictureDate.setValues(dateList);
        emailSmsCountInfos.forEach((value)->{
            smsDataList.add(value.getSmsCount());
        });
        pictureDataSms.setValues(smsDataList); //设置集合内容
        //查询 发送：打卡 比例
        for (int index=0;index<7;index++){
            Double aDouble = noteThingsService.queryPunchConvertSend(userID, index, 1, 1);
            if (aDouble == null)
                aDouble = 0.00;
            aDouble = Double.parseDouble(String.format("%.4f",aDouble)); //保留4为小树
            punchDataList.add(aDouble*100);
        }
        Collections.reverse(punchDataList);  //倒序输出
        pictureDataPunch.setValues(punchDataList); //设置打卡率
        //设置名称
        pictureDataEmail.setName("邮箱发送");
        pictureDataSms.setName("短信发送");
        //添加到返回值中
        pictureDataList.add(pictureDataEmail);
        pictureDataList.add(pictureDataSms);
        pictureDataList.add(pictureDataPunch);
        pictureDataList.add(pictureDate);
        return pictureDataList;
    }

    /**
     * 获取图3数据
     * @param userID
     * @return
     */
    @GetMapping("/getPictureThree/{uid}")
    public List<PictureDataInfo> queryThreePicture(@PathVariable("uid")String userID){
        List<PictureDataInfo> pictureDataInfos = new ArrayList<>();
        List<UserThings> userThings = userThingsService.queryThingsByUID(userID); //获取用户事件、
        Iterator<UserThings> userThingsIterator = userThings.iterator();
        while (userThingsIterator.hasNext()){
            UserThings things = userThingsIterator.next();
            PictureDataInfo pictureDataInfo = new PictureDataInfo();
            pictureDataInfo.setName(things.getNoteThings().getThingsType()==null?
                    "普通事件":things.getNoteThings().getThingsType()).setValue(1);
            pictureDataInfos.add(pictureDataInfo);
        }
        pictureDataInfos = pictureFactory.formatList(pictureDataInfos);
        return pictureDataInfos;
    }

    @GetMapping("/getPictureFour/{uid}")
    public List<PictureDataInfo> queryFourPicture(@PathVariable("uid")String userID){
        List<PictureDataInfo> pictureDataInfos = new ArrayList<>();
        PictureDataInfo pictureDataEmail = new PictureDataInfo();
        PictureDataInfo pictureDataSms = new PictureDataInfo();
        pictureDataEmail.setName("邮箱发送").setValue(noteThingsService.queryNumberByEmailAfter(userID,1));
        pictureDataSms.setName("短信发送").setValue(noteThingsService.queryNumberBySmsAfter(userID,1));
        pictureDataInfos.add(pictureDataEmail);
        pictureDataInfos.add(pictureDataSms);
        return pictureDataInfos;
    }
}
